package com.crowntit.domain.order.model.aggregate;

import com.crowntit.domain.order.model.entity.GroupBuyLockInfoEntity;
import com.crowntit.domain.order.model.entity.OrderEntity;
import com.crowntit.domain.order.model.entity.ProductEntity;
import com.crowntit.domain.order.model.valobj.OrderStatusVO;
import com.crowntit.types.enums.GroupBuyServiceResponseStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;
import java.util.Date;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreateOrderAggregate {

    private OrderEntity order;
    private ProductEntity product;
    private String userId;
    private GroupBuyLockInfoEntity groupBuyInfo;

    public static OrderEntity buildOrderEntity(String productId, String productName, BigDecimal totalAmount){
        return OrderEntity.builder()
                .productId(productId)
                .productName(productName)
                .totalAmount(totalAmount)
                .orderId(RandomStringUtils.randomNumeric(14))
                .orderTime(new Date())
                .status(OrderStatusVO.CREATE)
                .build();
    }

    public static GroupBuyLockInfoEntity buildGroupBuyInfoEntity(BigDecimal originalPrice, BigDecimal deductionPrice, BigDecimal payPrice, boolean isGroupBuy, GroupBuyServiceResponseStatusEnum groupBuyServiceResponseStatus) {
        Integer isGroupBuyOri = isGroupBuy ? 1 : 0;
        String groupBuyServiceResponseStatusInfo = null;
        if (groupBuyServiceResponseStatus != null) {
            groupBuyServiceResponseStatusInfo = groupBuyServiceResponseStatus.getInfo();
        }

        return GroupBuyLockInfoEntity.builder()
                .originalPrice(originalPrice)
                .deductionPrice(deductionPrice)
                .payPrice(payPrice)
                .isGroupBuy(isGroupBuyOri)
                .groupBuyServiceResponseStatus(groupBuyServiceResponseStatusInfo)
                .build();
    }
}
